/*
 * File     : Platform.java
 * Created  : 1 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.platform;

import java.util.Map;
import java.util.Set;

import com.googlecode.dni.internal.call.NativeCallFactory;
import com.googlecode.dni.internal.callback.ThunkManager.ThunkManagerDelegate;
import com.googlecode.dni.internal.library.DynamicLibrary;
import com.googlecode.dni.library.CallingConvention;
import com.googlecode.dni.platform.PlatformGroup;
import com.googlecode.dni.platform.PlatformType;
import com.googlecode.dni.type.Pointer;
import com.googlecode.dni.type.nativetype.NativeTypes;
import com.googlecode.dni.type.structure.Padding;


/**
 * <p>
 *  Provides an abstraction of platform-specific functionality, with the various
 *  bits needed for the internal implementation.
 * </p>
 *
 * @author Matthew Wilson
 */
public interface Platform
{

    /**
     * <p>
     *  Determines the platform group.
     * </p>
     *
     * @return the group
     */
    PlatformGroup getPlatformGroup();

    /**
     * <p>
     *  Determines the platform type.
     * </p>
     *
     * @return the type
     */
    PlatformType getPlatformType();

    /**
     * <p>
     *  Obtains a handle to the DNI dynamic library.
     * </p>
     *
     * @return a handle to the DNI dynamic library
     */
    DynamicLibrary getDniDynamicLibrary();

    /**
     * <p>
     *  Obtains a map of sizes (in bytes) of native sizes.
     * </p>
     * <p>
     *  For general use, see {@link NativeTypes}.
     * </p>
     *
     * @return a map of sizes
     */
    Map< Class< ? >, Integer > getNativeSizes();

    /**
     * <p>
     *  Obtains the default padding.
     * </p>
     *
     * @return the default padding
     */
    Padding getDefaultPadding();

    /**
     * <p>
     *  Obtains the default charset.
     * </p>
     *
     * @return the default charset
     */
    String getDefaultCharset();

    /**
     * <p>
     *  Obtains the wide charset.
     * </p>
     * <p>
     *  This should really be UTF-16 or UTF-32 in the platform's endian.
     * </p>
     *
     * @return the wide charset
     */
    String getWideCharset();

    /**
     * <p>
     *  Obtains supported calling conventions.
     * </p>
     *
     * @return supported calling conventions
     */
    Set< CallingConvention > getSupportedCallingConventions();

    /**
     * <p>
     *  Obtains the actual calling convention for the given convention.
     * </p>
     *
     * @param callingConvention
     *            the calling convention
     *
     * @return the actual calling convention,
     *         or <code>null</code> if not supported
     */
    CallingConvention getActualCallingConvention( CallingConvention callingConvention );

    /**
     * <p>
     *  Creates a native call factory for the given calling conventions.
     * </p>
     *
     * @param callingConvention
     *            the calling conventions
     *
     * @return a native call factory
     */
    NativeCallFactory createNativeCallFactory( CallingConvention callingConvention );

    /**
     * <p>
     *  Mangles (adds prefix and/or suffix) to the library name to make it match
     *  the platform's conventions.
     * </p>
     *
     * @param name
     *            the library name
     * @return the mangled library name
     */
    String mangleLibraryName( String name );

    /**
     * <p>
     *  Determines the size of a page of memory.
     * </p>
     *
     * @return the page size, in bytes
     */
    int getPageSize();

    /**
     * <p>
     *  Allocates pages of memory in the memory space of the process.
     * </p>
     *
     * @param size
     *            the size in bytes (should be a multiple of the page size,
     *            really)
     * @param executable
     *            whether the memory is executable (holding code)
     * @param writable
     *            whether the memory is writable
     *
     * @return a pointer to the base of the memory
     */
    Pointer allocatePages( int size, boolean executable, boolean writable );

    /**
     * <p>
     *  Modifies the flags of allocated pages.
     * </p>
     *
     * @param pointer
     *            the start pointer
     * @param size
     *            the size in bytes (should be a multiple of the page size,
     *            really)
     * @param executable
     *            whether the memory is executable (holding code)
     * @param writable
     *            whether the memory is writable
     */
    void protectPages( Pointer pointer, int size, boolean executable, boolean writable );

    /**
     * <p>
     *  Unallocates pages allocated by {@link #allocatePages(int, boolean, boolean)}.
     * </p>
     *
     * @param pointer
     *            the pointer
     * @param size
     *            the size
     */
    void unallocatePages( Pointer pointer, int size );

    /**
     * @param callingConvention
     *            the calling convention
     *
     * @return the delegate for handling thunks
     */
    ThunkManagerDelegate getThunkManagerDelegate( CallingConvention callingConvention );

}
